import java.util.Arrays;
import java.util.Scanner;

public class Main {
    //1.有一组数据，只有一个数字是出现一次，其他是两次，请找出这个数字。
    public static int findNum(int[] array) {
        int num = 0;
        for (int value : array) {
            num ^= value;//按位异或后赋值
        }
        return num;
    }

    public static void main1(String[] args) {
        int[] array = {1, 1, 2, 2, 3, 4, 4, 5, 5};
        System.out.println(findNum(array));
    }

    //2.求斐波那契数列的第n项。(迭代实现)
    public static int fib(int n) {
        int a = 1;
        int b = 1;
        int sum = 0;
        if (n == 1 || n == 2) {
            sum = n;
        }
        for (int i = 3; i <= n; i++) {
            sum = a + b;
            a = b;
            b = sum;
        }
        return sum;
    }

    public static void main2(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        System.out.println(fib(n));
    }

    //3.求1！+2！+3！+4！+........+n!的和
    public static int facSum(int num) {
        int sum = 1;
        for (int i = 1; i <= num; i++) {
            sum += fac(i);
        }
        return sum;
    }

    public static void main3(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        System.out.println(facSum(n));
    }

    //4.求 N 的阶乘
    public static int fac(int n) {
        int ret = 1;
        for (int i = 1; i <= n; i++) {
            ret *= i;
        }
        return ret;
    }

    public static void main4(String[] args) {
        Scanner scanner = new Scanner(System.in);
        int n = scanner.nextInt();
        System.out.println(fac(n));
    }

    //5.调整数组顺序使得奇数位于偶数之前。调整之后，不关心大小顺序。
    public static void order(int[] array) {
        int begin = 0;
        int end = array.length - 1;
        while (begin < end) {
            while (begin < end && (array[begin] & 1) != 0) {
                begin++;
            }
            while (begin < end && (array[end] & 1) == 0) {
                end--;
            }
            if (begin < end) {
                int temp = array[end];
                array[end] = array[begin];
                array[begin] = temp;
            }
        }
    }
    public static void main(String[] args) {
        int[] array = {1, 2, 3, 4, 5, 6};
        Main.order(array);
        System.out.println(Arrays.toString(array));
    }

    //6.创建方法求两个数的最大值max2，随后再写一个求3个数的最大值的函数max3。
    //              要求：在max3这个函数中，调用max2函数，来实现3个数的最大值计算
    public static int max2(int x,int y){
        return x>y?x:y;
    }
    public static int max3(int x,int y,int z){
        int temp = max2(x,y);
        return temp>z?temp:z;
    }

    public static void main6(String[] args) {
        System.out.println(max3(1,2,3));
    }

    //7.在同一个类中定义多个方法：要求不仅可以求两个整数的最大值，还可以求两个小数的最大值，以及两个小数和一个整数的大小关系
    public static int compare(int x,int y){
        return x>y?x:y;
    }
    public static double compare(double x,double y){
        return x>y?x:y;
    }
    public static double compare(double x,double y,int z){
        double temp1 = x>y?x:y;//求i和j中较大的
        double max = temp1>z?temp1:z;//求最大值；
        double temp2 = x<y?x:y;//求i和j中较小的；
        double min = temp1<z?temp1:z;//求最小值
        double mid = (x+y+z)-max-min;
        System.out.print(max+">"+mid+">"+min);
        return 0;
    }

    public static void main7(String[] args) {
        int a1=10;
        int b1=20;
        int ret1 = compare(a1, b1);
        System.out.println(ret1);
        double a2 = 15.5;
        double b2 = 25.5;
        double ret2 = compare(a2,b2);
        System.out.println(ret2);
        double ret3 = compare(a2,b2,a1);
        System.out.println(ret3);
    }
    //8.在同一个类中,分别定义求两个整数的方法 和 三个小数之和的方法。 并执行代码，求出结果
    public static int summation(int x,int y){
        return x+y;
    }
    public static double summation(double x,double y,double z){
        return x+y+z;
    }

    public static void main8(String[] args) {
        int a1=1;
        int b1=2;
        int ret1 = summation(a1, b1);
        System.out.println(ret1);
        double a2 = 1.5;
        double b2 = 2.5;
        double c2 = 3.5;
        double ret2 = summation(a2,b2,c2);
        System.out.println(ret2);
    }
    //9.递归 青蛙跳台阶问题
    public static int jumpFloor(int n){
        if(n<=0){
            return 0;
        }
        if(n==1){
            return 1;
        }
        return jumpFloor(n-1)+jumpFloor(n-2);
    }

    public static void main9(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        System.out.println(jumpFloor(n));
    }
    //10.递归 求解汉诺塔问题
    public static void move(char pos1,char pos2) {
        System.out.print(pos1+"->"+pos2+" ");

    }
    public static void hanoiTower(int n,char pos1,char pos2,char pos3){
        if(n==1){
            move(pos1,pos3);
            return;
        }
        hanoiTower(n-1,pos1,pos3,pos2);
        move(pos1,pos3);
        hanoiTower(n-1,pos2,pos1,pos3);
    }
    public static void main10(String[] args) {
        hanoiTower(1,'A','B','C');
        System.out.println();
        hanoiTower(2,'A','B','C');
        System.out.println();
        hanoiTower(3,'A','B','C');
    }
    //11.递归 求斐波那契数列的第 N 项
    public static int fib2(int n){
        int sum=0;
        if(n==1 || n==2){
            sum=n;
        }
        sum = fib2(sum-1)+fib2(sum-2);
        return sum;
    }
    public static void main11(String[] args){
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        System.out.println(fib(n));
    }
    //12.递归 输入一个非负整数，返回组成它的数字之和
    public static int sum(int n){
        if(n<0){
            System.out.println("输入错误，请输入非负整数：");
        }
        if(n < 9){
            return n;
        }
        return n%10 + sum(n/10);
    }
    public static void main12(String[] args){
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        System.out.println(sum(n));
    }
    //13.递归 按顺序打印一个数字的每一位(例如 1234 打印出 1 2 3 4)
    public static void print(int n){
        if(n > 9){
            print(n/10);
        }
        System.out.print(n%10+" ");
    }
    public static void main13(String[] args){
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        print(n);
    }
    //14.递归 求 1 + 2 + 3 + ... + 10
    public static int sum2(int n) {
        if(n==1){
            return 1;
        }
        return n + sum2(n-1);
    }
    public static void main14(String[] args){
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        System.out.println(sum2(n));
    }
    //15.递归 求 N 的阶乘
    public static int factor(int n){
        if(n==1){
            return 1;
        }
        return n * factor(n-1);
    }
    public static void main15(String[] args){
        Scanner scanner=new Scanner(System.in);
        int n=scanner.nextInt();
        System.out.println(factor(n));
    }
}
